package com.vladmr.sleepytimepicker;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class FallasleepDialogFragment11 extends DialogFragment {
	// protected AppPreferences appPrefs;
	// private static Context context;

	// public static FallasleepDialogFragment newInstance(Context c, int title)
	// {
	// FallasleepDialogFragment frag = new FallasleepDialogFragment();
	// Bundle args = new Bundle();
	// args.putInt("title", title);
	// frag.setArguments(args);
	// context = c;
	// return frag;
	// }

	private int position; // selected element in ChooseFallasleepActivity

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		final View view = getActivity().getLayoutInflater().inflate(
				R.layout.fallasleep_dialogfragment, null);

		Bundle args = getArguments();
		int title = args.getInt("title");
		position = args.getInt("position");
		if (args != null) {
			TextView tv_reminder = (TextView) view
					.findViewById(R.id.tv_reminder);
			TextView tv_alarm = (TextView) view.findViewById(R.id.tv_alarm);

			tv_reminder.setText(args.getString("reminder"));
			tv_alarm.setText(args.getString("alarm"));
		}

		return new AlertDialog.Builder(getActivity())
				.setIcon(R.drawable.ic_launcher)
				.setTitle(title)
				.setView(view)
				.setPositiveButton(R.string.btn_ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								positiveClick(view);
							}
						})
				.setNegativeButton(R.string.btn_cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								((ChooseFallasleepActivity) getActivity())
										.doNegativeDialogClick();
							}
						}).create();
	}

	private void positiveClick(View view) {
		CheckBox reminder = (CheckBox) view.findViewById(R.id.cb_reminder);
		CheckBox alarm = (CheckBox) view.findViewById(R.id.cb_alarm);
		((ChooseFallasleepActivity) getActivity()).doPositiveDialogClick(
				position, reminder.isChecked(), alarm.isChecked());
	}
}
